package hu.myai.calculators;

import hu.myai.model.db.AxHistory;
import hu.myai.model.misc.KeyValue;
import hu.myai.model.sp.StockPivot;

import java.util.ArrayList;
import java.util.List;

public class WilliamsR extends AbstractCalculator {

	private List<Double> williamsRList = new ArrayList<Double>();

	public WilliamsR(String symbol, StockPivot maxStock, StockPivot minStock, List<AxHistory> stockList,
			List<StockPivot> stockPivotList) {
		super(symbol, maxStock, minStock, stockList, stockPivotList);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int calculator(int mode) {

		int stockListSize = stockList.size();
		if (stockListSize > mode) {

			for (int i = 0; i < mode; i++) {
				williamsRList.add(i, null);
			}

			for (int i = mode; i < stockListSize; i++) {
				double[] minMax = getMinMaxStock(i - mode, i);

				williamsRList.add(i, (minMax[1] - stockList.get(i).getClose()) / (minMax[1] - minMax[0]) * -100);
			}
		} else
			return -1;

		return 0;
	}

	private double[] getMinMaxStock(int from, int to) {

		double lowerLow = stockList.get(from).getClose();
		double higherHigh = stockList.get(from).getClose();

		// get the highest and lowers value
		for (int i = (from + 1); i < to; i++) {
			double actClose = stockList.get(i).getClose();

			if (actClose > higherHigh)
				higherHigh = actClose;

			if (actClose < lowerLow)
				lowerLow = actClose;
		}

		double[] retValue = { lowerLow, higherHigh };
		return retValue;
	}

	@Override
	public KeyValue[] getNexts() {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Double> getWilliamsRList() {
		return williamsRList;
	}

}
